package com.schoolET.service.admin.adminImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolET.config.OssConfig;
import com.schoolET.entity.News;
import com.schoolET.entity.Region;
import com.schoolET.entity.dto.NewsDTO;
import com.schoolET.entity.vo.AddNewsVO;
import com.schoolET.entity.vo.NewsInfoVO;
import com.schoolET.error.costom.AdminNoRegionException;
import com.schoolET.error.costom.NoNewsException;
import com.schoolET.error.costom.NoRegionException;
import com.schoolET.error.costom.WechatNoRegionException;
import com.schoolET.mapper.NewsMapper;
import com.schoolET.mapper.RegionMapper;
import com.schoolET.service.admin.INewsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 * 新闻 服务实现类
 * </p>
 *
 * @author lin
 * @since 2024-06-02
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    @Resource
    private OssConfig ossConfig;

    @Resource
    private RegionMapper regionMapper;


    @Override
    public Page<NewsDTO> getNewsList(NewsInfoVO newsInfoVO) {
        if (newsInfoVO.getPageNum() == null || newsInfoVO.getPageSize() == null) {
            newsInfoVO.setPageNum(1);
            newsInfoVO.setPageSize(5);
        }
        Page<NewsDTO> newsPage = new Page<>(newsInfoVO.getPageNum(), newsInfoVO.getPageSize());
        return baseMapper.getNewsList(newsPage, newsInfoVO);
    }

    @Override
    @Transactional
    public boolean addNews(AddNewsVO addNewsVO, Integer adminId, MultipartFile file) throws IOException {
        News news = new News();
        BeanUtil.copyProperties(addNewsVO, news);
        String path = ossConfig.putObject(file, "news");
        news.setPicture(path);
//        Region region = regionMapper.getRegion(addNewsVO.getPosition());
        Region region = regionMapper.selectRegionByAdminId(adminId);
        if (region == null) {
            throw new NoRegionException();
        }
        baseMapper.insert(news);
        baseMapper.addNewsPosition(news.getId(), region.getId());
        return true;
    }

    @Override
    public NewsDTO getNewsInfoById(Integer id) {
        News news = baseMapper.selectById(id);
        if (news == null) {
            throw new NoNewsException();
        }
        NewsDTO newsDTO = new NewsDTO();
        BeanUtil.copyProperties(news, newsDTO);
        return newsDTO;
    }

    @Override
    public Page<NewsDTO> getNewsByOpenId(Integer pageNum, Integer pageSize, String openId) {
        Region region = regionMapper.selectRegionByOpenId(openId);
        if (region == null) {
            throw new WechatNoRegionException();
        }
        Page<NewsDTO> page = new Page<>(pageNum, pageSize);
        return baseMapper.getNewsListByRegionId(page, region.getId());
    }

    @Override
    public Page<NewsDTO> getNewsByAdminId(Integer pageNum, Integer pageSize, Integer adminId) {
        Region region = regionMapper.selectRegionByAdminId(adminId);
        if (region == null) {
            throw new AdminNoRegionException();
        }
        Page<NewsDTO> page = new Page<>(pageNum, pageSize);
        return baseMapper.getNewsListByRegionId(page, region.getId());
    }
}
